package org.magicbox.qjunTPO.service.impl;

import org.magicbox.qjunTPO.common.lang.Result;
import org.magicbox.qjunTPO.entity.*;
import org.magicbox.qjunTPO.mapper.SysMessageMapper;
import org.magicbox.qjunTPO.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 欢迎添加科技实践队粉丝群：690849711
 * @since 2021-11-18
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {

    @Override
    public boolean exResId(SysMessage sysMessage,
                           PoemsService poemsService,
                           ApplyAllService applyAllService,
                           CommentsService commentsService,
                           UserService userService) {
        switch (sysMessage.getType()){
            case 0:
                return (sysMessage.getUid()==null)&(sysMessage.getResId()==null);   //全体通知不允许指定用户id和resid
            case 1:
                //验证要发的系统通知目标用户和resid指向的用户是否一致
                Poems byId = poemsService.getById(sysMessage.getResId());
                if (byId==null) {
                    return false;
                }
                return byId.getUid().equals(sysMessage.getUid());
            case 2:
                ApplyAll applyAll = applyAllService.getById(sysMessage.getResId());
                if (applyAll==null) {
                    return false;
                }
                return (applyAll.getUid().equals(sysMessage.getUid()));
            case 3:
                Comments byId1 = commentsService.getById(sysMessage.getResId());
                if (byId1==null) {
                    return false;
                }
                return byId1.getUid().equals(sysMessage.getUid());
            case 4:
                User byId2 = userService.getById(sysMessage.getResId());
                if (byId2==null) {
                    return false;
                }
                return byId2.getId().equals(sysMessage.getUid());
            default: return  false;
        }
    }

    @Override
    public boolean addSysMsg(SysMessage sysMessage) {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        Integer suid =new Integer(authentication.getName());
//
//        sysMessage.setSuid(suid);
        sysMessage.setMsgCreate(LocalDateTime.now());
        try {
            save(sysMessage);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
